import re
import csv

bookmark_titles = []
page_numbers = []

with open('E:\\book.txt', 'r', encoding='utf-8') as file:
    lines = file.readlines()
    for index, line in enumerate(lines, start=1):
        print(f"正在处理第{index}行内容: {line.strip()}")
        # 通过查找最后面连续的数字作为页码部分，前面的作为书签内容部分
        match = re.search(r'(.*?)(\d+)$', line.strip())
        if match:
            title = match.group(1).strip()
            try:
                page = int(match.group(2))
                bookmark_titles.append(title)
                page_numbers.append(page)
            except ValueError:
                print(f"Warning: 无法将 {match.group(2)} 转换为有效的页码数字，跳过该行: {line.strip()}")
        else:
            print(f"Warning: 格式不符合预期，跳过该行: {line.strip()}")

# 将整理好的数据保存到CSV文件中
with open('bookmarks.csv', 'w', newline='', encoding='utf-8') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(['书签内容', '对应页码'])
    for title, page in zip(bookmark_titles, page_numbers):
        writer.writerow([title, page])